#include "htsc_grpc_provider_trace.h"
#include "grpc/support/tls.h"
#include "htsc_grpc_common.h"



//consumer 服务调用返回处理
void htsc_grpc_provider_callfinish(hstc_grpc_common_traceinfo_t *traceinfo, bool issuccess) {
	//把服务跟踪信息写入队列
	traceinfo->success = issuccess;
	htsc_grpc_trace_write_queue(traceinfo);

	//校验是否能够拿到threadlocal的内容
	//huyntodo 下面一行代码是测试代码，后面会删除
	hstc_grpc_trace_threadlocal_t *threadlocal = htsc_grpc_trace_threadlocal_getcurrenttrace();

	//清除threadlocal变量
	//非首节点在server端清除,同时清除下级C节点的threadlocal对象
	htsc_grpc_trace_threadlocal_clear();

	//printf("\n-------------- thread:%d ", GetCurrentThreadId());
	htsc_grpc_trace_free(&traceinfo);
}


/*
*func:根据provider端通用trace，生成流式推送trace信息。
*/
hstc_grpc_common_traceinfo_t *htsc_grpc_provider_newpushtrace(hstc_grpc_common_traceinfo_t *trace) {
	if (trace == NULL) {
		return NULL;
	}
	hstc_grpc_common_traceinfo_t *traceinfo = (hstc_grpc_common_traceinfo_t*)malloc(sizeof(hstc_grpc_common_traceinfo_t));
	size_t size = 37 * sizeof(char);
	char *new_traceid = (char *)malloc(size);
	memset(new_traceid, 0, size);
	htsc_grpc_uuid(new_traceid);
	//printf("\n----------------------------traceid : %s", new_traceid);
	traceinfo->traceid = new_traceid;
	traceinfo->parentchainid = "";                              //无需释放
	traceinfo->chainid = "0";                                   //无需释放
	traceinfo->callcount = 0;
	traceinfo->initial = true;
	traceinfo->servicename = trace->servicename;
	traceinfo->methodname = trace->methodname;
	traceinfo->success = true;
	traceinfo->consumerside = false;
	traceinfo->consumerhost = trace->consumerhost;                //无需释放
	traceinfo->consumerport = 0;                                  //无需释放
	traceinfo->providerhost = trace->providerhost;                //无需释放
	traceinfo->providerport = trace->providerport;
	traceinfo->protocol = HTSC_GRPC_TRACE_PROTOCOL;               //无需释放
	traceinfo->appname = htsc_get_provider_AppName();             //无需释放
	traceinfo->servicegroup = NULL;	                              //无需释放;
	traceinfo->serviceversion = htsc_grpc_version();  //无需释放;
	traceinfo->starttime = 0;
	traceinfo->writekafka = 1;
	traceinfo->pushtime = 0;
	new_traceid = NULL;
	return traceinfo;
}